package com.ccit.area.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ccit.area.service.AreaService;
import com.ccit.common.exception.GymException;
import com.ccit.common.result.Result;
import com.ccit.common.utils.RedisUtils;
import com.ccit.model.Area;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/area")
@Api(tags = "区域管理")
public class AreaController {

    @Autowired
    private AreaService areaService;
    @Autowired
    private RedisUtils redisUtils;

    @ApiOperation(value = "获取所有区域当前信息")
    @GetMapping("/getAllAreas")
    public Result getAreas() {
        List<Area> list;
        boolean has = redisUtils.hasKey("areaList");
        if (has) {
            list =(List<Area>) redisUtils.get("areaList");
        } else {
            //存储
            list = areaService.list();
            redisUtils.set("areaList", list, 600000);
        }
        if (list.size() == 0){
            throw new GymException(204,"没有区域");
        }
        return Result.ok(list);
    }

    @ApiOperation(value = "添加新区域")
    @PostMapping("/saveNewArea")
    public Result saveNewArea(@RequestBody Area area) {
        area.setCurrent(0);
        boolean flag = areaService.save(area);
        if (flag) {
            redisUtils.deleteRedisKeys("areaList");
            return Result.ok("添加成功");
        } else {
            return Result.fail().message("添加失败");
        }
    }

    @ApiOperation(value = "删除已停用区域")
    @DeleteMapping("/removeAreaById/{id}")
    public Result removeArea(@PathVariable Integer id) {
        boolean flag = areaService.removeById(id);
        if (flag) {
            redisUtils.deleteRedisKeys("areaList");
            return Result.ok("删除成功");
        } else {
            return Result.fail().message("删除失败");
        }
    }

    @ApiOperation(value = "更改区域信息")
    @PutMapping("/updateAreaInfo")
    public Result updateArea(@RequestBody Area area) {
        boolean flag = areaService.updateById(area);
        if (flag) {
            redisUtils.deleteRedisKeys("areaList");
            return Result.ok("修改成功");
        } else {
            return Result.fail().message("修改失败");
        }
    }

    @ApiOperation(value = "订单添加当前人数")
    @PutMapping("/updateAreaCurrentPerson/{name}/{num}")
    public boolean updateAreaPersonNumByName(@PathVariable String name,
                                             @PathVariable Integer num) {
        boolean result = areaService.updateCurrentNum(name, num);
        if (!result){
            throw new GymException(203,"服务异常");
        }else {
            redisUtils.deleteRedisKeys("areaList");
        }
        return result;
    }

    @ApiOperation(value = "区域人员离场登记")
    @PutMapping("/ReduceAreaCurrentPerson/{name}/{num}")
    public Result ReduceAreaPersonNumByName(@PathVariable String name,
                                            @PathVariable Integer num) {
        QueryWrapper<Area> wrapper = new QueryWrapper<>();
        wrapper.eq("name", name);
        Area area = areaService.getOne(wrapper);
        area.setCurrent(area.getCurrent() - num);
        boolean flag = areaService.updateById(area);
        if (flag) {
            redisUtils.deleteRedisKeys("areaList");
            return Result.ok(flag);
        } else
            return Result.fail(flag);
    }



}
